/*
*************************************************************************************************
*
*	Copyright (C), 2023-2050, ����
*
*	ģ������:   LCD����ģ��
*	�� �� ��:	LCD_HDTR024_240X320_UE.c
* 	��	  ��:	����
* 	��	  ��:	1.0
* 	˵	  ��:	����LCD_HDTR024_240X320_UE����������IC��ʼ�������ļ���
*				�ߴ�:	2.4��
*				�ֱ���:	240X320
*				����IC:	GC9307
*				ICͨѶ:	8λ16λ8080�ӿ�
*
*	�޸ļ�¼:	
*	   �汾��	����			����            ˵��
*      V1.0		2022-02-19   	����    		��ʼ�汾
*
*
**************************************************************************************************
*/
#include "LCD_HDTR035_320X480_TS.h"

#if(LCD_PANEL_TYPE == LCD_HDTR035_320X480_TS)

/*
***************************************
*	��  �ܣ�LCD��Ļ����
***************************************
*/
LcdTiming_T g_lcdTiming = {
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	
	0,
	0,
	0,
	0,
	0
};
#define HORIZONTAL_SCREEN	0
#define FRAME_RATE			60
LcdInfo_T	g_lcdInfo = {
#if	HORIZONTAL_SCREEN
	.pixelWidth		= 320,
	.pixelHeight	= 240,
#else
	.pixelWidth		= 240,
	.pixelHeight	= 320,
#endif	
	.wRamCmd		= 0X2C,
	.setXCmd		= 0X2A,
	.setYCmd		= 0X2B, 
	.colorFormat	= LCD_COLOR_FORMAT_RGB565,	
	.interType		= LCD_DRIVE_I8080,
#if	HORIZONTAL_SCREEN
#if (FRAME_RATE == 60)
	.frameRate		= 60,
	.clockDiv		= 4352
#else
	.frameRate		= 60,
	.clockDiv		= 8743
#endif
#else
#if (FRAME_RATE == 60)
	.frameRate		= 60,
	.clockDiv		= 3292
#else
	.frameRate		= 30,
	.clockDiv		= 6584
#endif
#endif

};

#define WriteAddr 			Lcd_WReg
#define WriteData 			Lcd_WData

void Lcd_Drive_Init(void)	
{
	////////// LCD init code ///////////////////

	WriteAddr(0xBB);
	WriteData(0x00);//0
	WriteData(0x00);//1
	WriteData(0x00);//2
	WriteData(0x00);//3
	WriteData(0x00);//4
	WriteData(0x00);//5
	WriteData(0x5A);//6
	WriteData(0xA5);//7

	WriteAddr(0xA0);
	WriteData(0x00);//0
	WriteData(0x10);//1
	WriteData(0x00);//2
	WriteData(0x02);//3
	WriteData(0x00);//4
	WriteData(0x00);//5
	WriteData(0x64);//6
	WriteData(0x3F);//7
	WriteData(0x00);//8
	WriteData(0x05);//9
	WriteData(0x3F);//10 cr_vcom_en_mode
	WriteData(0x3F);//11
	WriteData(0x00);//12
	WriteData(0x00);//13
	WriteData(0x00);//14
	WriteData(0x00);//15
	WriteData(0x00);//16

	WriteAddr(0xA2);
	WriteData(0x20);//0
	WriteData(0x3C);//1
	WriteData(0x29);//2
	WriteData(0x14);//3
	WriteData(0xA6);//4
	WriteData(0x3C);//5
	WriteData(0xA8);//6
	WriteData(0xE0);//7
	WriteData(0x40);//8
	WriteData(0x19);//9
	WriteData(0x80);//10
	WriteData(0x80);//11
	WriteData(0x80);//12
	WriteData(0x20);//13
	WriteData(0xf9);//14
	WriteData(0x10);//15 cr_dsprim_col_str,cr_dsprim_col_end
	WriteData(0x02);//16 cr_dsprim_row_str,cr_dsprim_row_end
	WriteData(0xff);//17 cr_pat_sel[15:8]
	WriteData(0xff);//18 cr_pat_sel[7:0]
	WriteData(0xF0);//19
	WriteData(0x90);//20
	WriteData(0x01);//21
	WriteData(0x32);//22
	WriteData(0xA0);//23
	WriteData(0x11);//24
	WriteData(0xE0);//25
	WriteData(0x20);//26
	WriteData(0x7F);//27
	WriteData(0xFF);//28
	WriteData(0x00);//29
	WriteData(0x04);//30

	WriteAddr(0xD0);
	WriteData(0xE0);//0
	WriteData(0x40);//1
	WriteData(0x51);//2
	WriteData(0x24);//3
	WriteData(0x08);//4
	WriteData(0x05);//5
	WriteData(0x10);//6
	WriteData(0x01);//7
	WriteData(0x90);//8
	WriteData(0x12);//9
	WriteData(0xC2);//10 cr_sd_en_sel
	WriteData(0x42);//11 cr_dual_type - dual gate
	WriteData(0x22);//12
	WriteData(0x22);//13
	WriteData(0xAA);//14
	WriteData(0x03);//15
	WriteData(0x10);//16
	WriteData(0x12);//17
	WriteData(0x60);//18 cr_dsh_shr
	WriteData(0x14);//19 cr_dual_pattern
	WriteData(0x1E);//20
	WriteData(0x51);//21
	WriteData(0x15);//22
	WriteData(0x00);//23
	WriteData(0x30);//24 cr_line_f_half[7:0]
	WriteData(0x15);//25 NC 1BIT,cr_sub_pix_ini,cr_line_f_half[9:8],
	WriteData(0x00);//26
	WriteData(0x03);//27
	WriteData(0x3D);//28
	WriteData(0x12);//29

	WriteAddr(0xA3);
	WriteData(0xA0);//0
	WriteData(0x06);//1
	WriteData(0xA9);//2
	WriteData(0xa8);//3 cr_svdd_bypdet
	WriteData(0x08);//4
	WriteData(0x02);//5
	WriteData(0x0A);//6
	WriteData(0x04);//7
	WriteData(0x04);//8
	WriteData(0x04);//9
	WriteData(0x04);//10
	WriteData(0x04);//11
	WriteData(0x04);//12
	WriteData(0x04);//13
	WriteData(0x04);//14
	WriteData(0x04);//15
	WriteData(0x04);//16
	WriteData(0x04);//17
	WriteData(0x04);//18
	WriteData(0x00);//19
	WriteData(0x55);//20
	WriteData(0x55);//21


	WriteAddr(0xC1);
	WriteData(0x31);//0 cr_mipi_ckln_reset??cr_src_opt??cr_bgr_opt??cr_ret
	WriteData(0x04);//1
	WriteData(0x02);//2
	WriteData(0x02);//3
	WriteData(0x71);//4
	WriteData(0x05);//5
	WriteData(0x24);//6
	WriteData(0x55);//7
	WriteData(0x00);//8
	WriteData(0x00);//9
	WriteData(0x01);//10
	WriteData(0x01);//11
	WriteData(0x53);//12
	WriteData(0xFF);//13
	WriteData(0xFF);//14
	WriteData(0xFF);//15
	WriteData(0x4F);//16
	WriteData(0x52);//17
	WriteData(0x00);//18
	WriteData(0x4F);//19
	WriteData(0x52);//20
	WriteData(0x00);//21
	WriteData(0x55);//22
	WriteData(0x3B);//23
	WriteData(0x0B);//24
	WriteData(0x02);//25
	WriteData(0x0f);//26
	WriteData(0x00);//27
	WriteData(0xFF);//28
	WriteData(0x40);//29

	WriteAddr(0xC3);
	WriteData(0x00);//0
	WriteData(0x00);//1
	WriteData(0x00);//2
	WriteData(0x50);//3
	WriteData(0x03);//4
	WriteData(0x00);//5
	WriteData(0x00);//6
	WriteData(0x00);//7
	WriteData(0x01);//8
	WriteData(0x80);//9
	WriteData(0x01);//10

	WriteAddr(0xC4);
	WriteData(0x00);//0
	WriteData(0x24);//1
	WriteData(0x33);//2
	WriteData(0x90);//3 cr_vcom_en_mode
	WriteData(0x00);//4 cr_vcom_vsel
	WriteData(0xEA);//5
	WriteData(0x64);//6
	WriteData(0x32);//7
	WriteData(0xC8);//8
	WriteData(0x64);//9
	WriteData(0xC8);//10
	WriteData(0x32);//11
	WriteData(0x90);//12
	WriteData(0x90);//13
	WriteData(0x11);//14
	WriteData(0x06);//15
	WriteData(0xDC);//16
	WriteData(0xFA);//17
	WriteData(0x54);//18 cr_vsp_clk_mode_at_tp
	WriteData(0x03);//19 vcom_at tp gamma_at_tp
	WriteData(0x80);//20
	WriteData(0xFE);//21
	WriteData(0x10);//22
	WriteData(0x10);//23
	WriteData(0x00);//24
	WriteData(0x0A);//25
	WriteData(0x0A);//26
	WriteData(0x44);//27
	WriteData(0x50);//28

	WriteAddr(0xC5);
	WriteData(0x18);//0
	WriteData(0x00);//1
	WriteData(0x00);//2
	WriteData(0x03);//3
	WriteData(0xFE);//4
	WriteData(0x50);//5 cr_te_str[7:0]
	WriteData(0x38);//6 cr_te_end[7:0]
	WriteData(0x20);//7 cr_te_str[11:8],cr_te_end[11:8]
	WriteData(0x30);//8
	WriteData(0x10);//9
	WriteData(0x88);//10
	WriteData(0xDE);//11
	WriteData(0x0D);//12
	WriteData(0x08);//13
	WriteData(0x0F);//14
	WriteData(0x0F);//15
	WriteData(0x01);//16
	WriteData(0x50);//17 cr_tp_str[7:0]
	WriteData(0x38);//18 cr_tp_end[7:0]
	WriteData(0x20);//19 cr_tp_str[11:8],cr_tp_end[11:8]
	WriteData(0x10);//20
	WriteData(0x10);//21
	WriteData(0x00);//22

	WriteAddr(0xC6);
	WriteData(0x05);//0
	WriteData(0x0A);//1
	WriteData(0x05);//2
	WriteData(0x0A);//3
	WriteData(0x00);//4
	WriteData(0xE0);//5
	WriteData(0x2E);//6
	WriteData(0x0B);//7
	WriteData(0x12);//8
	WriteData(0x22);//9
	WriteData(0x12);//10
	WriteData(0x22);//11
	WriteData(0x01);//12
	WriteData(0x03);//13
	WriteData(0x00);//14
	WriteData(0x02);//15
	WriteData(0x6A);//16
	WriteData(0x18);//17
	WriteData(0xC8);//18
	WriteData(0x22);//19

	WriteAddr(0xC7);
	WriteData(0x50);//0
	WriteData(0x32);//1 cr_lvd18_en cr_lvd30_en
	WriteData(0x28);//2
	WriteData(0x00);//3
	WriteData(0xa2);//4 cr_lvd18_adj cr_lvd30_adj
	WriteData(0x80);//5
	WriteData(0x8f);//6
	WriteData(0x00);//7
	WriteData(0x80);//8
	WriteData(0xff);//9
	WriteData(0x07);//10
	WriteData(0x11);//11
	WriteData(0x9c);//12
	WriteData(0x67);//13
	WriteData(0xff);//14
	WriteData(0x24);//15 cr_igzo_timing_en
	WriteData(0x0c);//16
	WriteData(0x0d);//17
	WriteData(0x0e);//18
	WriteData(0x0f);//19

	WriteAddr(0xC9);
	WriteData(0x33);//0
	WriteData(0x44);//1
	WriteData(0x44);//2
	WriteData(0x01);//3

	WriteAddr(0xCF);
	WriteData(0x34);//0 cr_vrefn_en_mode
	WriteData(0x1E);//1
	WriteData(0x88);//2
	WriteData(0x58);//3
	WriteData(0x13);//4
	WriteData(0x18);//5
	WriteData(0x56);//6
	WriteData(0x18);//7
	WriteData(0x1E);//8
	WriteData(0x6d);//9
	WriteData(0x8C);//10
	WriteData(0x00);//11
	WriteData(0x65);//12
	WriteData(0x09);//13
	WriteData(0x22);//14
	WriteData(0xC4);//15
	WriteData(0x0C);//16
	WriteData(0x77);//17
	WriteData(0x22);//18
	WriteData(0x44);//19
	WriteData(0xAA);//20
	WriteData(0x55);//21
	WriteData(0x04);//22
	WriteData(0x04);//23
	WriteData(0x0b);//24
	WriteData(0xA0);//25
	WriteData(0x08);//26

	WriteAddr(0xD5);
	WriteData(0x00);//0 cr_gspf2
	WriteData(0x3e);//1 cr_gsps2
	WriteData(0x89);//2 cr_vst_pre_adj
	WriteData(0x01);//3 cr_vst_post_adj
	WriteData(0x35);//4 cr_hig_wdt cr_low_wdt
	WriteData(0x04);//5 cr_rst_wdt
	WriteData(0x92);//6 cr_gpwr1_star[7:0]
	WriteData(0x6F);//7 cr_gpwr1_end[7:0]
	WriteData(0x04);//8 cr_gpwr1_star[11:8]cr_gpwr1_end[11:8]
	WriteData(0x92);//9 cr_gpwr2_star[7:0]
	WriteData(0x6F);//10 cr_gpwr2_end[7:0]
	WriteData(0x04);//11 cr_gpwr2_star[11:8] cr_gpwr2_end[11:8]
	WriteData(0x08);//12 cr_dum_num
	WriteData(0x6A);//13 cr_rst_star[7:0]
	WriteData(0x04);//14 cr_gpwr1_sel,cr_gpwr2_sel,cr_fv,cr_frm_sel,cr_rst_star[11:8]
	WriteData(0x46);//15 cr_fl,cr_gck_bgn
	WriteData(0x03);//16 cr_gp1r
	WriteData(0x03);//17 cr_gp1f
	WriteData(0x03);//18 cr_gp2r
	WriteData(0x03);//19 cr_gp2f
	WriteData(0x00);//20 cr_gck_en_adj
	WriteData(0x01);//21 cr_vg_fix_high[7:0]
	WriteData(0x03);//22 cr_vg_fix_low[7:0]
	WriteData(0x00);//23 cr_vg_fix_high[11:8],cr_vg_fix_low[11:8]
	WriteData(0xE0);//24 cr_nl[7:0]
	WriteData(0x51);//25 cr_gip_igzo_opt cr_nl[11:8]
	WriteData(0xa1);//26 cr_bp A1-A7
	WriteData(0x00);//27 cr_gspf1
	WriteData(0x00);//28 cr_gsps1
	WriteData(0x00);//29 cr_gto_max


	WriteAddr(0xD6);
	WriteData(0x10);//0
	WriteData(0x32);//1
	WriteData(0x54);//2
	WriteData(0x76);//3
	WriteData(0x98);//4
	WriteData(0xBA);//5
	WriteData(0xDC);//6
	WriteData(0xFE);//7
	WriteData(0x93);//8
	WriteData(0x00);//9
	WriteData(0x01);//10
	WriteData(0x83);//11
	WriteData(0x07);//12
	WriteData(0x07);//13
	WriteData(0x00);//14
	WriteData(0x07);//15
	WriteData(0x07);//16
	WriteData(0x00);//17
	WriteData(0x03);//18
	WriteData(0x03);//19
	WriteData(0x03);//20
	WriteData(0x03);//21
	WriteData(0x03);//22
	WriteData(0x03);//23
	WriteData(0x00);//24
	WriteData(0x84);//25
	WriteData(0x00);//26
	WriteData(0x20);//27
	WriteData(0x01);//28
	WriteData(0x00);//29

	WriteAddr(0xD7);
	WriteData(0x03);//0
	WriteData(0x01);//1
	WriteData(0x0b);//2
	WriteData(0x09);//3
	WriteData(0x0f);//4
	WriteData(0x0d);//5
	WriteData(0x1E);//6
	WriteData(0x1F);//7
	WriteData(0x18);//8
	WriteData(0x1d);//9
	WriteData(0x1f);//10
	WriteData(0x19);//11
	WriteData(0x00);//12
	WriteData(0x3e);//13
	WriteData(0x04);//14
	WriteData(0x00);//15 cr_gip_hs_dual1[11:8],cr_gip_hs_dual2[11:8]
	WriteData(0x1d);//16 cr_gip_hs_dual1[7:0]
	WriteData(0x40);//17 cr_gip_hs_dual2[7:0]
	WriteData(0x1F);//18

	WriteAddr(0xD8);
	WriteData(0x02);//0
	WriteData(0x00);//1
	WriteData(0x0a);//2
	WriteData(0x08);//3
	WriteData(0x0e);//4
	WriteData(0x0c);//5
	WriteData(0x1E);//6
	WriteData(0x1F);//7
	WriteData(0x18);//8
	WriteData(0x1d);//9
	WriteData(0x1f);//10
	WriteData(0x19);//11

	WriteAddr(0xD9);
	WriteData(0x1F);//0
	WriteData(0x1F);//1
	WriteData(0x1F);//2
	WriteData(0x1F);//3
	WriteData(0x1F);//4
	WriteData(0x1F);//5
	WriteData(0x1F);//6
	WriteData(0x1F);//7
	WriteData(0x1F);//8
	WriteData(0x1F);//9
	WriteData(0x1F);//10
	WriteData(0x1F);//11

	WriteAddr(0xDD);
	WriteData(0x1F);//0
	WriteData(0x1F);//1
	WriteData(0x1F);//2
	WriteData(0x1F);//3
	WriteData(0x1F);//4
	WriteData(0x1F);//5
	WriteData(0x1F);//6
	WriteData(0x1F);//7
	WriteData(0x1F);//8
	WriteData(0x1F);//9
	WriteData(0x1F);//10
	WriteData(0x1F);//11

	WriteAddr(0xDF);
	WriteData(0x44);//0
	WriteData(0x33);//1
	WriteData(0x4B);//2
	WriteData(0x69);//3
	WriteData(0x00);//4
	WriteData(0x0A);//5
	WriteData(0x02);//6
	WriteData(0x90);//7

	WriteAddr(0xE0);
	WriteData(0x3B);//0  vgmp
	WriteData(0x28);//1  j0p V0P      255
	WriteData(0x0f);//2  v1p          251
	WriteData(0x14);//3  v2p          247
	WriteData(0x0c);//4  v4p          239
	WriteData(0x03);//5  v6p          231
	WriteData(0x11);//6  v13p         203
	WriteData(0x26);//7  v20p         179
	WriteData(0x4b);//8  v27p v36p    147   111
	WriteData(0x21);//9  v43p         83
	WriteData(0x0d);//10 v50p         55
	WriteData(0x36);//11 v57p         27
	WriteData(0x13);//12 v59p         19
	WriteData(0x2a);//13 v61p         11
	WriteData(0x2f);//14 v62p         7
	WriteData(0x28);//15 j1p v63p     3
	WriteData(0x0D);//16 vgsp

	WriteAddr(0xE1);
	WriteData(0x37);//0  vgmn
	WriteData(0x28);//1  j0n V0n      255
	WriteData(0x0f);//2  v1n          251
	WriteData(0x14);//3  v2n          247
	WriteData(0x0b);//4  v4n          239
	WriteData(0x03);//5  v6n          231
	WriteData(0x11);//6  v13n         203
	WriteData(0x26);//7  v20n         179
	WriteData(0x4b);//8  v27n v36n    147   111
	WriteData(0x21);//9  v43n         83
	WriteData(0x0d);//10 v50n         55
	WriteData(0x36);//11 v57n         27
	WriteData(0x13);//12 v59n         19
	WriteData(0x2a);//13 v61n         11
	WriteData(0x2D);//14 v62n         7
	WriteData(0x28);//15 j1n v63n     3
	WriteData(0x0F);//16 vgsn

	WriteAddr(0xE2);
	WriteData(0x3B);//0
	WriteData(0x07);//1
	WriteData(0x12);//2
	WriteData(0x18);//3
	WriteData(0x0E);//4
	WriteData(0x0D);//5
	WriteData(0x17);//6
	WriteData(0x35);//7
	WriteData(0x44);//8
	WriteData(0x32);//9
	WriteData(0x0C);//10
	WriteData(0x14);//11
	WriteData(0x14);//12
	WriteData(0x36);//13
	WriteData(0x3A);//14
	WriteData(0x2F);//15
	WriteData(0x0D);//16

	WriteAddr(0xE3);
	WriteData(0x37);//0
	WriteData(0x07);//1
	WriteData(0x12);//2
	WriteData(0x18);//3
	WriteData(0x0E);//4
	WriteData(0x0D);//5
	WriteData(0x17);//6
	WriteData(0x35);//7
	WriteData(0x44);//8
	WriteData(0x32);//9
	WriteData(0x0C);//10
	WriteData(0x14);//11
	WriteData(0x14);//12
	WriteData(0x36);//13
	WriteData(0x32);//14
	WriteData(0x2F);//15
	WriteData(0x0F);//16

	WriteAddr(0xE4);
	WriteData(0x3B);//0
	WriteData(0x07);//1
	WriteData(0x12);//2
	WriteData(0x18);//3
	WriteData(0x0E);//4
	WriteData(0x0D);//5
	WriteData(0x17);//6
	WriteData(0x39);//7
	WriteData(0x44);//8
	WriteData(0x2E);//9
	WriteData(0x0C);//10
	WriteData(0x14);//11
	WriteData(0x14);//12
	WriteData(0x36);//13
	WriteData(0x3A);//14
	WriteData(0x2F);//15
	WriteData(0x0D);//16

	WriteAddr(0xE5);
	WriteData(0x37);//0
	WriteData(0x07);//1
	WriteData(0x12);//2
	WriteData(0x18);//3
	WriteData(0x0E);//4
	WriteData(0x0D);//5
	WriteData(0x17);//6
	WriteData(0x39);//7
	WriteData(0x44);//8
	WriteData(0x2E);//9
	WriteData(0x0C);//10
	WriteData(0x14);//11
	WriteData(0x14);//12
	WriteData(0x36);//13
	WriteData(0x3A);//14
	WriteData(0x2F);//15
	WriteData(0x0F);//16

	WriteAddr(0xBB);
	WriteData(0x00);//0
	WriteData(0x00);//1
	WriteData(0x00);//2
	WriteData(0x00);//3
	WriteData(0x00);//4
	WriteData(0x00);//5
	WriteData(0x00);//6
	WriteData(0x00);//7


	WriteAddr(0x11);
	HAL_Delay(200);

	WriteAddr(0x29);
	HAL_Delay(100);

}

#endif

